// Appendix3.1

log using Appendix3.1.log, replace

use "C:\Users\sbstjp\OneDrive - Cardiff University\anes_timeseries_2020_stata_20220210.dta" // American National Election Study 2020 Time Series Study, Feb 10, 2022 Version. Date accessed: March 09, 2025. Accessed from https://electionstudies.org/data-center/2020-time-series-study/

// Social justice scale 
*Delete missing values
rename V201411x tgpolicy 
replace tgpolicy = . if tgpolicy == -2
rename V201626 offence 
replace offence = . if inlist(offence, -5, -9)
rename V202183 metoo 
replace metoo = . if inlist(metoo, -9, -7, -6, -5, -4, 998, 999)
rename V202174 blm 
replace blm = . if inlist(blm, -9, -7, -6, -5, -4, 998, 999)

*Reverse code offence so social justice values are high
egen maxval = max(offence)
gen roffence = maxval + 1 - offence
drop maxval

*Standardize items in the scale from 1-2 - this avoids 0, for reasons outlined in next step
foreach var in tgpolicy roffence metoo blm {
    summarize `var'
    gen s`var' = 1 + (`var' - r(min)) / (r(max) - r(min))
}

* Replace missing values with 0 for the specified variables - this is necessary as Stata doesn't add up missing values and means a 0-1 standardization scale isn't feasible as missing values would overlap with the scale
foreach var in stgpolicy sroffence smetoo sblm  {
    replace `var' = 0 if missing(`var')
}

* Initialize the total score and the count of non-zero responses
gen total_scoreSJV = 0
gen count_nonzeroSJV = 0

* Add each variable to the total scale score and count it if non-zero
foreach var in stgpolicy sroffence smetoo sblm  {
    replace total_scoreSJV = total_scoreSJV + `var'
    replace count_nonzeroSJV = count_nonzeroSJV + (`var' != 0)
}

* Calculate the average score, avoiding division by zero
gen socJusValues = .
replace socJusValues = total_scoreSJV / count_nonzeroSJV if count_nonzeroSJV > 0

// Liberalism scale
*Delete missing values

rename V201415 gayadopt
replace gayadopt = . if inlist(gayadopt, -8, -9)
rename V201336 abortion
replace abortion = . if inlist(abortion, -8, -9, 5) 
rename V201262 environment
replace environment = . if inlist(environment, -9, -8, 99)
rename V202232 immigration
replace immigration = . if inlist(immigration, -9, -8, -7, -6, -5) 
rename V201345x deathpen
replace deathpen = . if inlist(deathpen, -2) 
rename V201426x wall
replace wall = . if inlist(wall, -2) 
rename V201308x bordersec
replace bordersec = . if inlist(bordersec, -2) 
rename V201311x crime
replace crime = . if inlist(crime, -2) 

*Reverse variables so liberalism coded high
foreach var in gayadopt immigration environment {
    qui sum `var'
    local max_value = r(max)
    gen r`var' = `max_value' + 1 - `var'
}

*Standardize items in the scale from 1-2 - this avoids 0, for reasons outlined in next step
foreach var in rgayadopt abortion renvironment rimmigration deathpen wall bordersec crime {
    summarize `var'
    gen s`var' = 1 + (`var' - r(min)) / (r(max) - r(min))
}

* Replace missing values with 0 for the specified variables - this is necessary as Stata doesn't add up missing values and means a 0-1 standardization scale isn't feasible as missing values would overlap with the scale
foreach var in srgayadopt sabortion srenvironment srimmigration sdeathpen swall sbordersec scrime {
    replace `var' = 0 if missing(`var')
}

* Initialize the total score and the count of non-zero responses
gen total_scoreAL = 0
gen count_nonzeroAL = 0

* Add each variable to the total scale score and count it if non-zero
foreach var in srgayadopt sabortion srenvironment srimmigration sdeathpen swall sbordersec scrime {
    replace total_scoreAL = total_scoreAL + `var'
    replace count_nonzeroAL = count_nonzeroAL + (`var' != 0)
}

* Calculate the average score, avoiding division by zero
gen libValues = .
replace libValues = total_scoreAL / count_nonzeroAL if count_nonzeroAL > 0

// Demographic variables
* Delete missing values
rename V201507x age 
replace age = . if age == -9

rename V201617x income 
replace income = . if inlist(income, -9, -5)

rename V201510 education 
replace education = . if inlist(education, -9, -8, 95)

rename V201600 FemaleGender 
replace FemaleGender = . if FemaleGender == -9

rename V201549x race
replace race = . if inlist(race, -9, -8)

rename V201200 libconsp 
replace libconsp = . if inlist(libconsp, -9, -8, 99)

*Create dummy and centred-squared variables
gen Graduate=.
replace Graduate=0 if education<6
replace Graduate=1 if inrange(education, 6, 8)

egen age_centered = mean(age) 
replace age_centered = age - age_centered
gen age_centered_squared = age_centered^2

gen Unemployed = . 
replace Unemployed = 1 if inlist(V201533x, 20, 40)
replace Unemployed = 0 if V201533x >= 0 & (V201533x < 20)
replace Unemployed = 0 if inlist(V201533x, 51, 61, 71, 81)

gen Married=.
replace Married=1 if V201508==1
replace Married=0 if inrange(V201508, 2, 6)

//LifeSat
*Delete missing values
rename V201651 lifesat 
replace lifesat = . if inlist(lifesat, -9, -8, -5)

*Now reverse, so happiness is coded high
qui sum lifesat 
local max_value = r(max)
gen reversed_lifesat = `max_value' + 1 - lifesat

*Standardize from 1-2, so it's like other dependent variables in the book
foreach var in reversed_lifesat {
    summarize `var'
    gen s`var' = 1 + (`var' - r(min)) / (r(max) - r(min))
}

rename sreversed_lifesat LifeSat

// Religious Attendance
gen ReligiousAtt=. 
replace ReligiousAtt=0 if V201454==-1
replace ReligiousAtt=1 if inlist(V201454, 1, 2)

// Standardize variables
egen Income = std(income)
egen Age = std(age_centered_squared) 
egen LibValues = std(libValues)
egen SocJusValues = std(socJusValues)

regress LifeSat SocJusValues [pweight=V200010b], robust 
eststo
regress LifeSat SocJusValues Age FemaleGender Graduate Income Married ReligiousAtt Unemployed [pweight=V200010b], robust 
eststo
regress LifeSat LibValues [pweight=V200010b], robust 
eststo
regress LifeSat LibValues Age FemaleGender Graduate Income Married ReligiousAtt Unemployed [pweight=V200010b], robust 
eststo
regress LifeSat SocJusValues LibValues Age FemaleGender Graduate Income Married ReligiousAtt Unemployed [pweight=V200010b], robust 
eststo
esttab

log close

eststo clear

esttab using "Appendix3.1.rtf", ///
    b(3) se(3) ///
    star(* 0.05 ** 0.01 *** 0.001) ///
    coeflabels(_cons "Constant") ///
    nonumbers replace

// Alternative specification
ologit LifeSat SocJusValues Age FemaleGender Graduate Income Married ReligiousAtt Unemployed [pweight=V200010b], robust // alternative specification

// Coefficient plot 
regress LifeSat SocJusValues LibValues Age FemaleGender Graduate Income Married ReligiousAtt Unemployed [pweight=V200010b], robust 
label variable Age ""
label variable Income ""
label variable FemaleGender ""
label variable SocJusValues ""
label variable LibValues ""
coefplot, drop(_cons) xline(0, lcolor(red) lwidth(medium)) scheme(white_jet) xtitle("{bf: Life Satisfaction}")


